package com.three.storemanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.Station;
import com.three.core.entity.User;
import com.three.storemanage.feign.UserFeign;
import com.three.storemanage.mapper.StationMapper;
import com.three.storemanage.service.StationService;
import com.three.core.entity.Store;
import com.three.core.vo.R;
import com.three.storemanage.vo.SearchForm;
import com.three.storemanage.vo.StationVo;
import com.three.core.entity.Station;
import com.three.storemanage.mapper.StationMapper;
import com.three.storemanage.mapper.StoreMapper;
import com.three.storemanage.service.StationService;
import com.three.storemanage.vo.StationVo;
import com.three.core.vo.R;
import com.three.storemanage.vo.SearchForm;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: StationServiceImpl
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/11 14:41
 * @Version: 1.0.0
 */
@Service
public class StationServiceImpl implements StationService {
    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserFeign userFeign;

    @Override
    public R<Page<StationVo>> findStationByCondition(SearchForm searchForm) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        Page<StationVo> page = new Page<>(searchForm.getCurrentPage(), searchForm.getPageSize());
        Page<StationVo> records = stationMapper.findStationByCondition(page,searchForm.getStoreName(),userId);
        return new R<>(200,"查询成功",records);
    }

    @Override
    public R deleteStationById(Integer station) {
        int i=stationMapper.deleteById(station);
        if(i>0){
            return new R<>(200,"删除成功",null);
        }else{
            return new R<>(400,"删除失败",null);
        }
    }

    @Override
    public R addStation(Station station) {
        int i=stationMapper.insertSelective(station);
        if(i>0){
            return new R<>(200,"添加成功",null);
        }else{
            return new R<>(400,"添加失败",null);
        }
    }

    @Override
    public R updateStation(Station station) {
        int i=stationMapper.updateByPrimaryKeySelective(station);
        if(i>0){
            return new R<>(200,"修改成功",null);
        }else{
            return new R<>(400,"修改失败",null);
        }
    }

    @Override
    public R<List<Store>> queryAll(Double userLng, Double userLat, String storeName) {
        List<Store> stores = storeMapper.queryAll(userLng, userLat,storeName);
        return new R<>(200,"查询成功",stores);
    }

    @Override
    public R<List<Store>> queryAll2(Double userLng, Double userLat) {
        List<Store> stores = storeMapper.queryAll2(userLng, userLat);
        return new R<>(200,"查询成功",stores);
    }

    @Override
    public R<List<Store>> selectStoreByUserId() {
        //获取token
        String token = request.getHeader("token");
        //获取用户名
        Object user = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) user);
        User user1 = feign.getData();
        Integer userId = user1.getUserId();
        List<Store> stores = storeMapper.selectStoreByUserId((String) user,userId);
        return new R<>(200,"查询成功",stores);
    }

    @Override
    public R<List<Station>> selectStationByStoreId(Integer storeId) {
        List<Station> stations = stationMapper.selectByStoreId(storeId);
        return new R<>(200,"查询成功",stations);
    }
}